package com.jiang.booksystem.controller;

import com.jiang.booksystem.core.ApiResponse;
import com.jiang.booksystem.core.UserTokenContainer;
import com.jiang.booksystem.dao.entity.Book;
import com.jiang.booksystem.dao.entity.User;
import com.jiang.booksystem.dao.entity.UserBookBorrow;
import com.jiang.booksystem.dao.entity.dos.BookAggre;
import com.jiang.booksystem.dao.entity.dos.UserBookBorrowAggre;
import com.jiang.booksystem.manager.BookManager;
import com.jiang.booksystem.manager.UserBookBorrowManager;
import com.jiang.booksystem.manager.UserManager;
import com.jiang.booksystem.utils.HttpRequestUtils;
import com.jiang.booksystem.vo.BookAggreVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * 首页控制器
 */

@RestController
public class HomeController {

    @Autowired
    BookManager bookManager;
    @Autowired
    UserManager userManager;
    @Autowired
    UserBookBorrowManager userBookBorrowManager;

    @GetMapping("/")
    public ModelAndView index() {
        // ModelAndView : 表示的是模型的数据 和 视图文件地址
        ModelAndView mv = new ModelAndView("index");
        mv.addObject("name", "jiang");
        return mv;
    }


    @GetMapping("/view/books")
    public ModelAndView booklist(HttpServletRequest request, Integer userId) {

        String token = HttpRequestUtils.getValueFromCookieByKey(request, "token");
        if (token == null) {
            return new ModelAndView(new RedirectView("/view/login"));
        }
        User user = UserTokenContainer.get(token);
        if (user == null) {
            return new ModelAndView(new RedirectView("/view/login"));
        }

        ModelAndView mv = new ModelAndView("booklist");
        List<BookAggre> books = bookManager.search(userId);

        List<BookAggreVo> bookAggreVoList = new ArrayList<>();
        for (BookAggre bookAggre : books) {
            BookAggreVo vo = new BookAggreVo();
            BeanUtils.copyProperties(bookAggre, vo);
            vo.setStatusDisplay(buildStatusDisplay(bookAggre.getStatus()));
            bookAggreVoList.add(vo);
        }

        mv.addObject("books", bookAggreVoList);
        mv.addObject("user", user);

        return mv;
    }

    @GetMapping("/view/books/add")
    public ModelAndView addBook() {
        return new ModelAndView("bookadd");
    }

    //编辑书本
    @GetMapping("/view/books/edit")
    public ModelAndView bookedit(Integer id) {
        ModelAndView mv = new ModelAndView("bookedit");
        Book book = bookManager.getByBookId(id);
        mv.addObject("book", book);
        return mv;
    }

    @GetMapping("view/books/stock")
    public ModelAndView bookstock(Integer id) {
        ModelAndView mv = new ModelAndView("bookstock");
        Book book = bookManager.getByBookId(id);
        mv.addObject("book", book);
        return mv;
    }

    @GetMapping("/view/users")
    public ModelAndView userlist(HttpServletRequest request) {
        ModelAndView mv = new ModelAndView("user");

        String token = HttpRequestUtils.getValueFromCookieByKey(request, "token");
        if (token == null) {
            return new ModelAndView(new RedirectView("/view/login"));
        }
        User user = UserTokenContainer.get(token);
        if (user == null) {
            return new ModelAndView(new RedirectView("/view/login"));
        }

        Integer filterUserId = null;
        if (!user.isAdmin()) {
            filterUserId = user.getId();
        }

        List<User> users = userManager.search(filterUserId);

        mv.addObject("users", users);
        return mv;
    }

    @GetMapping("/view/users/add")
    public ModelAndView addUser() {
        return new ModelAndView("useradd");
    }

    @GetMapping("view/users/edit")
    public ModelAndView useredit(Integer id) {
        ModelAndView mv = new ModelAndView("useredit");
        User user = userManager.getByUserId(id);
        mv.addObject("user", user);
        return mv;
    }

    @GetMapping("view/users/password")
    public ModelAndView userpassword(Integer id) {
        ModelAndView mv = new ModelAndView("userpassword");
        User user = userManager.getByUserId(id);
        mv.addObject("user", user);
        return mv;
    }


    @GetMapping("/view/login")
    public ModelAndView login() {
        return new ModelAndView("login");
    }


    @GetMapping("view/users/borrow")
    public ModelAndView borrow(int userId) {
        ModelAndView mv = new ModelAndView("borrow");
        List<UserBookBorrowAggre> data = userManager.searchApply(userId);

        mv.addObject("data", data);

        return mv;

    }

    private String buildStatusDisplay(Integer status) {
        if (status == null) {
            return "未借阅";
        }

        switch (status) {
            case 0:
                return "审核中";
            case 1:
                return "已借阅";
            default:
                return "已拒绝";
        }
    }
}
